
import java.io.*;
import java.net.*;

public class DownloadFile {

    public DownloadFile() {
        this.download("lgmt".toUpperCase());
    }

    public boolean download(String airport) {
        DataInputStream bin = null;
        DataOutputStream bout = null;
        try {
            URL url = new URL("ftp://tgftp.nws.noaa.gov/data/observations/metar/decoded/" + airport + ".TXT");
            URLConnection connection = url.openConnection();
            bin = new DataInputStream(new BufferedInputStream(connection.getInputStream()));
            String fileName = airport + ".txt";
            bout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(fileName)));
            int length = connection.getContentLength();
            int count = 0;
            while (count++ < length) {
                bout.write(bin.read());
            }
            bin.close();
            bout.flush();
            bout.close();
        } catch (EOFException eofexc) {
            try {
                bin.close();
                bout.flush();
                bout.close();
            } catch (IOException ioexc) {
                System.out.println("copyFile: IOException: " + ioexc);
            }
        } catch (IOException ioexc) {
            System.out.println("copyFile: IOException: " + ioexc);
        }
        return true;
    }
//
//    public static void main(String[] args) {
//        new DownloadFile();
//    }
}
